package com.telecom.weixi.util;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.util.Log;
import android.widget.Toast;

public class Utils {
	private static Toast sToast;
	
	public static void toast(Context context, String info, boolean isLong){
		if(sToast == null){
			if(isLong){
				sToast = Toast.makeText(context, info, Toast.LENGTH_LONG);
			}else{
				sToast = Toast.makeText(context, info, Toast.LENGTH_SHORT);
			}
		}
		sToast.setText(info);
		sToast.show();
	}
	
	public static void debug(String tag, String info){
		Log.d(tag, info);
	}
	
	public static InputStream String2InputStream(String stream){
		ByteArrayInputStream is = null;
		try {
			is = new ByteArrayInputStream(stream.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			debug("test", "err " + e.getMessage());
		} 
		return is;
	}
	public static boolean isNetAvailable(Context context){
		boolean netSataus = false;
		ConnectivityManager cwjManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		cwjManager.getActiveNetworkInfo();

		if (cwjManager.getActiveNetworkInfo() != null) {
			netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
		}
		return netSataus;
	}
   
	public static void setNetWork(Context context, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancelListener){
		AlertDialog.Builder b = new AlertDialog.Builder(context).setTitle(
				"没有可用的网络").setMessage("是否对网络进行设置？");
		b.setPositiveButton("是", okListener)
				.setNeutralButton("否", cancelListener).show();
	}
	
	public static void showErrorDialog(Context context, String warning, String btnTxt, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(warning);
		builder.setCancelable(false)
			.setIcon(0)
			.setPositiveButton(btnTxt, listener);
		builder.show();
	}
	
	public static int compareCurDate(int year,int month,int day){
		Calendar c = Calendar.getInstance();
		int calendarYear = c.get(Calendar.YEAR);
		int calendarMonth = c.get(Calendar.MONTH);
		int calendarDay = c.get(Calendar.DAY_OF_MONTH);
		int deltaYear = year - calendarYear;
		if(deltaYear != 0)
			return deltaYear;
		int deltaMon = month - calendarMonth;
		if(deltaMon != 0)
			return deltaMon;
		return day - calendarDay;
	}
	
	public static void writeFileData(String message, boolean append){
		try {
			FileOutputStream output = new FileOutputStream("/sdcard/weixi.txt", append);
			byte[] bytes = message.getBytes();
			output.write(bytes);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
